//
//  ReserveScrowView.m
//  SimpleHappy
//
//  Created by lanou on 15/11/26.
//  Copyright © 2015年 RockyFung. All rights reserved.
//

#import "ReserveScrowView.h"
#import "SDCycleScrollView.h"
#import "Define.h"
#import "UIImageView+WebCache.h"



@interface ReserveScrowView ()

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView; // 添加滚动视图
@property (nonatomic, strong) UILabel *title; // 标题
@property (nonatomic, strong) UILabel *tagsLabel; // 标签
@property (nonatomic, strong) UIImageView *hostNameIcon; // 店家图标
@property (nonatomic, strong) UILabel *hostName; // 店名
@property (nonatomic, strong) UIImageView *addressIcon; // 地点图标
@property (nonatomic, strong) UILabel *address; // 具体地点
@property (nonatomic, strong) UIImageView *numberIcon; // 电话图标
@property (nonatomic, strong) UILabel *number; // 电话

@property (nonatomic, strong) UIImageView *playHeaderIcon; // 玩乐表头图片

@end






@implementation ReserveScrowView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        CGFloat picWidth = frame.size.width;
        CGFloat picHeight = picWidth / 750 * 500;
        
        // 二十
        CGFloat ershi = KScreenWidth / 18.75;

        // 添加轮播图片
        self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, picWidth, picHeight) imageURLStringsGroup:nil]; // 模拟网络延时情景
        self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        self.cycleScrollView.dotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
        self.cycleScrollView.placeholderImage = [UIImage imageNamed:@"placeholder"];
        self.cycleScrollView.autoScrollTimeInterval = 3;
        [self addSubview:_cycleScrollView];

        
//        CGFloat labelWidth = picWidth - 20;
        // 标题
        self.title = [[UILabel alloc]initWithFrame:CGRectZero];
        self.title.font = [UIFont boldSystemFontOfSize:ershi];
        self.title.numberOfLines = 0;
        [self addSubview:_title];
        
        // 标签
        self.tagsLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        self.tagsLabel.text = @"";
        self.tagsLabel.textColor = [UIColor grayColor];
        self.tagsLabel.numberOfLines = 0;
        self.tagsLabel.font = [UIFont systemFontOfSize:KScreenWidth / 25];
        [self addSubview:_tagsLabel];
        
        // 店家
        self.hostNameIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dianming"]];
        [self addSubview:_hostNameIcon];
        
        self.hostName = [[UILabel alloc]initWithFrame:CGRectZero];
        self.hostName.textColor = [UIColor grayColor];
        self.hostName.text = @"";
        self.hostName.font = [UIFont systemFontOfSize:KScreenWidth / 22];
        [self addSubview:_hostName];
        
        // 地址
        self.addressIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dizhi"]];
        [self addSubview:_addressIcon];
        
        self.address = [[UILabel alloc]initWithFrame:CGRectZero];
        self.address.text = @"";
        self.address.font = [UIFont systemFontOfSize:KScreenWidth / 22];
        self.address.textColor = [UIColor grayColor];
        self.address.numberOfLines = 0;
        [self addSubview:_address];
        
        // 电话
        self.numberIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dianhua"]];
        [self addSubview:_numberIcon];
        
        self.number = [[UILabel alloc]initWithFrame:CGRectZero];
        self.number.text = @"";
        self.number.font = [UIFont systemFontOfSize:KScreenWidth / 22];
        self.number.textColor = [UIColor grayColor];
        [self addSubview:_number];
    }
    return self;
}

// 根据字符串和字体求字符串的高度
- (CGSize)stringSizeWithFont:(UIFont *)font string:(NSString *)string width:(CGFloat)width
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    return rect.size;
}


- (void)setModel:(ReserveModel *)model
{
    _model = model;
    
    CGFloat ershi = KScreenWidth / 18.75;
    
////////////////表头图片
    // 如果是玩家页面跳转过来的
    if (_isPlay) {
        [self.cycleScrollView removeFromSuperview];
        CGFloat picHeight = KScreenWidth / 640 * 330;
        self.playHeaderIcon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, picHeight)];
        [self.playHeaderIcon sd_setImageWithURL:[NSURL URLWithString:self.model.picUrl]];
        [self addSubview:_playHeaderIcon];
    }else{
        // 设置轮播图片
        self.cycleScrollView.imageURLStringsGroup = model.headPics;
    }
    
    
    
//////////////// 标题
    self.title.text = model.title;
    // 可变标题高度
        CGSize titleHeight = [self stringSizeWithFont:[UIFont systemFontOfSize:ershi] string:model.title width:KScreenWidth - ershi];
        self.title.frame = CGRectMake(ershi/2, KScreenHeight / 2.55 , KScreenWidth - ershi, titleHeight.height);
    if (_isPlay) {
        CGSize titleHeight = [self stringSizeWithFont:[UIFont systemFontOfSize:ershi] string:model.title width:KScreenWidth - ershi];
        self.title.frame = CGRectMake(ershi/2, KScreenHeight / 3.2 , KScreenWidth - ershi, titleHeight.height);
    }
    
    
//////////////// 标签
    // 拼接数组里的字符串
    NSMutableString *ss = [NSMutableString stringWithString:@"·"];
    for (NSString *str in model.tags) {
        [ss  appendFormat:@"%@·",str];
    }
    self.tagsLabel.text = ss;
    CGSize tagHeight = [self stringSizeWithFont:[UIFont systemFontOfSize:ershi-5] string:ss width:KScreenWidth - ershi];
    self.tagsLabel.frame = CGRectMake (ershi/2, self.title.frame.origin.y + titleHeight.height + ershi/2 , KScreenWidth - ershi, tagHeight.height);

    
///////////////// 店家
    if (_isPlay) {
        [self.hostName removeFromSuperview];
        self.hostNameIcon.frame = CGRectMake (ershi/2, self.title.frame.origin.y + titleHeight.height + tagHeight.height + ershi , ershi, ershi);
        self.hostName.frame = CGRectMake (ershi*2 + 5, self.title.frame.origin.y + titleHeight.height + tagHeight.height + ershi , KScreenWidth - ershi*3, ershi+5);
//        self.hostName.text = model;
        
    }else{
        self.hostNameIcon.frame = CGRectMake (ershi/2, self.title.frame.origin.y + titleHeight.height + tagHeight.height + ershi , ershi, ershi);
        self.hostName.frame = CGRectMake (ershi*2 + 5, self.title.frame.origin.y + titleHeight.height + tagHeight.height + ershi , KScreenWidth - ershi*3, ershi+5);
        self.hostName.text = model.hostName;
    }
    
    
///////////////// 地址
    self.addressIcon.frame = CGRectMake (ershi/2 , self.title.frame.origin.y + titleHeight.height + ershi/2 + tagHeight.height + ershi*2+5, ershi, ershi);
    CGSize addressHeight = [self stringSizeWithFont:[UIFont systemFontOfSize:ershi-3] string:model.address width:KScreenWidth - ershi*3];
    self.address.frame = CGRectMake (ershi*2 + 5, self.title.frame.origin.y + titleHeight.height + ershi/2 + tagHeight.height + ershi*2+5, KScreenWidth - ershi*3, addressHeight.height);
    self.address.text = model.address;
    
///////////////// 电话
    self.numberIcon.frame = CGRectMake (ershi/2 , self.title.frame.origin.y + titleHeight.height + tagHeight.height + addressHeight.height + ershi*3.5, ershi+5, ershi+5);
    self.number.frame = CGRectMake (ershi*2+5 , self.title.frame.origin.y + titleHeight.height + tagHeight.height + addressHeight.height + ershi*3.5, KScreenWidth - ershi * 3, ershi + 5);
    self.number.text = model.contactNumber;
    
    if (_isPlay) {
        [self.tagsLabel removeFromSuperview];
        [self.hostName removeFromSuperview];
        [self.hostNameIcon removeFromSuperview];
        [self.addressIcon removeFromSuperview];
        [self.address removeFromSuperview];
        [self.number removeFromSuperview];
        [self.numberIcon removeFromSuperview];
    }

}




@end
